from AIAssistant import (
    AIAssistant,
    DataAnalysisAssistant,
    ImageGenerationAssistant,
    TextGenerationAssistant,
    TranslationAssistant
)
from AIManager import AIManager


def create_assistant_by_type(type_choice, aid, name, version):
    """根据类型创建助手"""
    if type_choice == 1:
        speed = input("请输入数据处理速度(默认: 中等): ") or "中等"
        return DataAnalysisAssistant(aid, name, version, speed)
    elif type_choice == 2:
        quality = input("请输入图像质量(默认: 高): ") or "高"
        return ImageGenerationAssistant(aid, name, version, quality)
    elif type_choice == 3:
        fluency = input("请输入文本流畅度(默认: 流畅): ") or "流畅"
        return TextGenerationAssistant(aid, name, version, fluency)
    elif type_choice == 4:
        try:
            languages = int(input("请输入支持语言数量(默认: 20): ") or "20")
        except ValueError:
            languages = 20
        return TranslationAssistant(aid, name, version, languages)
    else:
        # 默认创建基础AI助手
        return AIAssistant(aid, name, "通用助手", version)


def main():
    """主程序入口"""
    manager = AIManager()

    while True:
        print("\n========== AI助手管理器 ==========")
        print("1. 查看所有AI助手列表")
        print("2. 根据ID查询助手详情")
        print("3. 添加新的AI助手")
        print("4. 根据ID修改助手信息")
        print("5. 删除AI助手（按ID）")
        print("6. 启动AI助手服务")
        print("7. 停止AI助手服务")
        print("8. 为AI助手分配任务")
        print("9. 显示系统统计信息")
        print("0. 退出系统")
        print("===================================")

        choice = input("请选择操作: ")

        if choice == '1':
            manager.list_all_assistants()
        elif choice == '2':
            try:
                aid = int(input("请输入要查询的AI助手ID: "))
                ast = manager.find_assistant_by_id(aid)
                if ast:
                    details = ast.get_details()
                    print(
                        f" 查询结果: ID: {details['ID']}, 名称: {details['名称']}, 类型: {details['类型']}, 版本: {details['版本']}, 状态: {details['状态']}, 能力评分: {details['能力评分']}")
                else:
                    print("未找到该助手")
            except ValueError:
                print("无效输入")
        elif choice == '3':
            try:
                print("添加新AI助手")
                aid = int(input("请输入ID: "))
                name = input("请输入名称: ")
                version = input("请输入版本号: ")
                is_online = bool(int(input("是否在线？(1-是, 0-否): ")))
                score = float(input("请输入能力评分(0-100): "))

                # 创建基础助手
                new_ast = AIAssistant(aid, name, "通用助手", version, is_online, score)
                manager.add_assistant(new_ast)
            except ValueError:
                print("无效输入")
        elif choice == '4':
            try:
                print("修改AI助手数据")
                aid = int(input("请输入ID: "))
                print("可修改属性列表为：name,type,version,online,score")
                attr_input = input("请输入要要修改的属性和值，用空格分割: ")
                parts = attr_input.split()
                if len(parts) >= 2:
                    attribute, value = parts[0], " ".join(parts[1:])
                    manager.update_assistant(aid, attribute, value)
                else:
                    print("输入格式错误")
            except ValueError:
                print("无效输入")
        elif choice == '5':
            try:
                aid = int(input("请输入要删除的AI助手ID: "))
                manager.remove_assistant_by_id(aid)
            except ValueError:
                print("无效输入")
        elif choice == '6':
            try:
                aid = int(input("请输入要启动的AI助手ID: "))
                ast = manager.find_assistant_by_id(aid)
                if ast:
                    if ast.is_online:
                        print(f"助手【{ast.name}】已在运行。")
                    else:
                        ast.start_service()
                        print(f"助手【{ast.name}】启动成功。")
                else:
                    print("未找到该助手")
            except ValueError:
                print("无效输入")
        elif choice == '7':
            try:
                aid = int(input("请输入要停止的AI助手ID: "))
                ast = manager.find_assistant_by_id(aid)
                if ast:
                    if not ast.is_online:
                        print(f"助手【{ast.name}】已停止。")
                    else:
                        ast.stop_service()
                        print(f"助手【{ast.name}】已停止。")
                else:
                    print("未找到该助手")
            except ValueError:
                print("无效输入")
        elif choice == '8':
            try:
                aid = int(input("请输入目标AI助手ID: "))
                ast = manager.find_assistant_by_id(aid)
                if ast:
                    task_desc = input("请输入任务内容: ")
                    result = ast.assign_task(task_desc)
                    print(result)
                else:
                    print("未找到该助手")
            except ValueError:
                print("无效输入")
        elif choice == '9':
            manager.display_statistics()
        elif choice == '0':
            print("感谢使用！再见~")
            break
        else:
            print("无效选项，请重新选择")


if __name__ == "__main__":
    main()
